package com.leetcode.No0671;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @program: leetcode
 * @description: 二叉树中第二小的节点
 * @author: wangzhihua
 * @date: 2021-07-27
 */
public class Solution01 {

	private TreeSet<Integer> set = new TreeSet<Integer>(new Comparator<Integer>() {
		public int compare(Integer o1, Integer o2) {
			return o1 - o2;
		}
	});

	public int findSecondMinimumValue(TreeNode root) {
		dfs(root);
		if (set.size() == 1) {
			return -1;
		}
		set.remove(root.val);
		return set.first();
	}

	public void dfs(TreeNode node) {
		if (node.left == null) {
			set.add(node.val);
			return;
		}
		dfs(node.left);
		dfs(node.right);
	}

	public class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;

		TreeNode() {
		}

		TreeNode(int val) {
			this.val = val;
		}

		TreeNode(int val, TreeNode left, TreeNode right) {
			this.val = val;
			this.left = left;
			this.right = right;
		}
	}

}
